package com.power.using.mybomb.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.power.using.mybomb.R;
import com.power.using.mybomb.adapter.OnRecyclerViewListener;
import com.power.using.mybomb.adapter.SearchUserAdapter;
import com.power.using.mybomb.base.ParentWithNaviActivity;
import com.power.using.mybomb.bean.User;
import com.power.using.mybomb.model.BaseModel;
import com.power.using.mybomb.model.UserModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by 聪明一只哈 on 2020/3/23.
 * Time: 2020/3/23  14:21
 */
public class SearchUserActivity extends ParentWithNaviActivity {

    @BindView(R.id.v_top)
    View vTop;
    @BindView(R.id.tv_left)
    ImageView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_find_name)
    EditText etFindName;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.rc_view)
    RecyclerView rcView;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout swRefresh;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    private SearchUserAdapter adapter;
    private LinearLayoutManager layoutManager;

    @Override
    protected String title() {
        return "搜索用户";
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_user);
        ButterKnife.bind(this);
        initNaviView();
        adapter = new SearchUserAdapter();
        layoutManager = new LinearLayoutManager(this);
        rcView.setLayoutManager(layoutManager);
        rcView.setAdapter(adapter);

        swRefresh.setEnabled(true);
        swRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                query();
            }
        });

        adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() {
            @Override
            public void onItemClick(int position) {
                Bundle bundle = new Bundle();
                User user = adapter.getItem(position);
                bundle.putSerializable("u",user);
                startActivity(UserInfoActivity.class,bundle,false);
            }

            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });

    }



    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        swRefresh.setRefreshing(true);
        query();
    }



    private void query() {

        String name = etFindName.getText().toString();
        if(TextUtils.isEmpty(name)){
            toast("请填写用户名");
            swRefresh.setRefreshing(false);
            return;
        }

        UserModel.getInstance().queryUsers(name, BaseModel.DEFAULT_LIMIT,
                new FindListener<User>() {
                    @Override
                    public void done(List<User> list, BmobException e) {
                        if(e==null){
                            swRefresh.setRefreshing(false);
                            adapter.setDatas(list);
                            adapter.notifyDataSetChanged();
                        }else{
                            swRefresh.setRefreshing(false);
                            adapter.setDatas(null);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

    }
}
